import { mapMutations, mapActions, mapState } from "vuex";
import { typeClassName } from "@/pages/config/classNameMap";
import _ from "lodash";
const requireComponent = require.context("./", true, /[A-Z]\w+\.(vue)$/);
const comp = {};
requireComponent.keys().forEach((fileName) => {
  if (fileName != "./index.vue") {
    const componentConfig = requireComponent(fileName);
    const name = componentConfig.default.name;
    const componentName = _.camelCase(
      name
        .split("/")
        .pop()
        .replace(/\.\w+$/, "")
    );
    comp[componentName] = componentConfig.default || componentConfig;
  }
});
Object.keys(comp).map((key) => {
  comp[key].components = comp;
});
export default {
  components: comp,
  data() {
    return {
      vms: {}, //页面组件vue实例
      completeLoad: false,
      me: this,
      watchs: [],
      //模型
      models: {
        //模型

        productTtype: {
          ...typeClassName["productTtype"],
        },
      },
      tabsIndex: null,
      rowId: null,
      dataSet: {
        //数据集
        currentData: {},
        formData: {
          sf_code: "",
          sf_name: "",
        },
        tableData: [],
      },
    };
  },
  provide() {
    return {
      anc: this,
    };
  },
  computed: {},
  created() {
    Object.keys(this.models).map((k) => {
      this.models[k].clz = this.$modelObject.create(this.models[k].modelType);
      this.models[k].clzs = this.$modelObject.create(
        this.models[k].modelType + "Collection"
      );
    });
  },
  mounted() {
    this.watchs.map((item) => {
      this.$watch(item.target, item.fn, { deep: item.deep });
    });
    this.$nextTick(() => {
      console.log(comp, "comp");
      this.unwatch = this.$watch("vms", (newValue, oldValue) => {
        let flag = true;
        Object.keys(comp).map((k) => {
          if (!Object.keys(this.vms).includes(k)) {
            flag = false;
          }
        });
        this.completeLoad = flag;
      });
      this.init();
      if (process.env.NODE_ENV === "development") {
        console.log({
          me: this,
          vms: this.vms,
          completeLoad: this.completeLoad,
        });
      }
    });
  },
  destroyed() {
    this.unwatch();
  },
  methods: {
    init() {
      // this.getlabelKnowledge();
    },
    loadPage(pageNavigation, filter) {
      let newFilter = {
        ...filter,
        className: this.models.productTtype.className,
        type: this.models.productTtype.type,
      };
      this._$queryByPage(
        this.models["productTtype"].clzs,
        pageNavigation,
        newFilter,
        "nodeId != 'root'",
        "updateTime",
        false
      ).then((res) => {
        this.dataSet.tableData = [];
        this.models["productTtype"].clzs.valueRecords[0].hier.content.forEach(
          (d) => {
            console.log(d);
            this.dataSet.tableData.push({
              ...d,
              ...d.values,
              createTime: this.models["productTtype"].clzs.valueRecords[0].createTime,
              updateTime: this.models["productTtype"].clzs.valueRecords[0].updateTime,
              createdBy: this.models["productTtype"].clzs.valueRecords[0].createdBy,
              updatedBy: this.models["productTtype"].clzs.valueRecords[0].updatedBy,
            });
          }
        );
      });
    },
    /**
     * 新增-弹窗显示
     */
    handleCreate() {
      this._modalShow("createModal");
      this.vms.createView.resetFields();
    },
    /**
     * 查看-弹窗显示
     * @param {*} row
     */
    handleDetail(row) {
      this._modalShow("detailModal");
      this.dataSet.currentData = row;
      this.dataSet.formData = {
        ...row.values,
      };
    },
    /**
     * 编辑-弹窗显示
     * @param {*} row
     */
    handleEdit(row) {
      this._modalShow("editModal");
      this.dataSet.currentData = row;
      // this.dataSet.formData = row;
      this.dataSet.formData = {
        ...row.values,
      };
    },
    /**
     * 删除单行
     * @param {行数据} row
     * @param {索引} index
     */
    handleDelete(row, index) {
      this._$deleteConfirm(() => {
        this.$api.HierInstanceDeleteHier([row.id]).then((flag) => {
          this.vms.mainTable.navigation.page.pageNumber = 1;
          this.loadPage(this.vms.mainTable.navigation.page, {});
        });
      });
    },
    // 新增确认
    createModalOk(btn) {
      console.log(this.dataSet.formData.values);
      this._$validateAll([this.vms.createView], (valid) => {
        if (valid) {
          let newData = {
            values: this.dataSet.formData,
            containerModel: this.models.productTtype.modelType,
            containerId: this.models["productTtype"].clzs.valueRecords[0].id,
            parentId: "root",
          };

          this.$api.HierInstanceSaveHier([newData]).then((flag) => {
            if (flag) {
              this.vms.mainTable.navigation.page.pageNumber = 1;
              this.loadPage(this.vms.mainTable.navigation.page, {});
              this._modalClose("createModal");
            }
            btn.loading = false;
          });
        } else {
          btn.loading = false;
          this.$Message.info("请填写必填内容");
        }
      });
      btn.loading = false;
    },
    // 编辑确认
    editModalOk(btn) {
      this.vms.editView.validate((valid) => {
        btn.loading = false;
        if (valid) {
          let newData = {
            values: this.dataSet.formData,
            containerModel: this.models.productTtype.modelType,
            containerId: this.models["productTtype"].clzs.valueRecords[0].id,
            parentId: "root",
            id: this.dataSet.currentData.id,
          };

          this.$api.HierInstanceSaveHier([newData]).then((flag) => {
            this.vms.mainTable.navigation.page.pageNumber = 1;
            this.loadPage(this.vms.mainTable.navigation.page, {});
            this._modalClose("editModal");
          });
        } else {
          btn.loading = false;
          this._$requireInfo();
        }
      });
      btn.loading = false;
    },
  },
};
